<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['audit:node:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
      <el-button
        type="primary"
        plain
        size="mini"
        @click="getList"
      >刷新</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="nodeList">
      <el-table-column label="审核顺序" align="center" prop="auditSort" class-name="allowDrag"/>
      <el-table-column label="节点名称" align="center" prop="nodeName" class-name="allowDrag"/>
      <el-table-column label="角色名称" align="center" prop="roleName" class-name="allowDrag"/>
      <el-table-column label="备注" align="center" prop="remark" class-name="allowDrag"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width allowDrag" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['audit:node:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['audit:node:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单支出费用审核对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="上一节点" >
          <el-select clearable
            v-model="form.beforeId"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="item in sortList"
              :key="item.id"
              :label="item.node_name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="节点名称" prop="nodeName">
          <el-input v-model="form.nodeName" placeholder="请输入节点名称" />
        </el-form-item>
        <el-form-item label="角色" prop="roleId">
          <el-select
            v-model="form.roleId"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="item in roleOptions"
              :key="item.roleId"
              :label="item.roleName"
              :value="item.roleId"
              :disabled="item.status == 1"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listNode, getNode, delNode, addNode, updateNode,getSortList } from "@/api/audit/node";
import {getUser} from "@/api/system/user";
export default {
  name: "Node",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单支出费用审核表格数据
      nodeList: [],
      sortList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        auditSort: null,
        nodeName: null,
        tenantId: null,
        roleId: null,
        roleName: null
      },
      // 角色选项
      roleOptions: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        nodeName: [
          { required: true, message: '节点名称不能为空', trigger: 'blur' },
          {
            min: 2,
            max: 20,
            message: '节点名称长度必须介于 2 和 20 之间',
            trigger: 'blur',
          },
        ],
        roleId: [{ required: true, message: '角色不能为空', trigger: 'blur' }],
      }
    };
  },
  created() {
    this.getList();
    getUser().then((response) => {
      this.roleOptions = response.roles
    })
  },
  methods: {
    /** 查询订单支出费用审核列表 */
    getList() {
      this.loading = true;
      listNode(this.queryParams).then(response => {
        this.nodeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        auditSort: null,
        nodeName: null,
        tenantId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null,
        roleId: null,
        roleName: null,
        beforeId: null
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加审核节点";
      this.getSortList(-1);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.sortList=[];
      const id = row.id || this.ids
      getNode(id).then(response => {
        this.getSortList(id);
        this.form = response.data;
        console.log('---------------', this.form)
        this.open = true;
        this.title = "修改审核节点";
      });
    },
    /** 节点明细 */
    getSortList(id) {
      getSortList(id).then(response => {
        this.sortList = response.data;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          debugger
          let obj = {};
          obj = this.roleOptions.find((item)=>{//model就是上面的数据源
            return item.roleId === this.form.roleId;//筛选出匹配数据
          });
          this.form.roleName= obj.roleName;
          if (this.form.id != null) {
            updateNode(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNode(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除审核节点编号为"' + ids + '"的数据项？').then(function() {
        return delNode(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
